
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>ksnowlv</title>
  <meta name="author" content="律威">

  
  <meta name="description" content="Drawable 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 &hellip;">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://ksnowlv.github.io">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="ksnowlv" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="//ajax.useso.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.useso.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.useso.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">

  
  <script type="text/javascript">
    var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-39546615-2']);
    // _gaq.push(['_trackPageview']);

    // (function() {
    //   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    //   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    //   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    // })();

    _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src='http://droidyue-tools.qiniudn.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
  </script>


</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">ksnowlv</a></h1>
  
    <h2>回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="http://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:ksnowlv.github.io" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
  <li><a href="/">博客首页</a></li>
  <li><a href="/blog/archives">文章列表</a></li>
  <!-- <li><a href="/about-me">关于我</a></li>
 -->
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div class="blog-index">
  
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2017/04/26/android-drawable/">Android Drawable</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2017-04-26T11:55:00+08:00" pubdate data-updated="true">Apr 26<span>th</span>, 2017</time>
        
         | <a href="/blog/2017/04/26/android-drawable/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>Drawable</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">public</span> <span class="k">static</span> <span class="n">Drawable</span> <span class="n">createFromXmlInner</span><span class="p">(</span><span class="n">Resources</span> <span class="n">r</span><span class="p">,</span> <span class="n">XmlPullParser</span> <span class="n">parser</span><span class="p">,</span> <span class="n">AttributeSet</span> <span class="n">attrs</span><span class="p">,</span>
</span><span class='line'>            <span class="n">Theme</span> <span class="n">theme</span><span class="p">)</span> <span class="n">throws</span> <span class="n">XmlPullParserException</span><span class="p">,</span> <span class="n">IOException</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">final</span> <span class="n">Drawable</span> <span class="n">drawable</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">final</span> <span class="n">String</span> <span class="n">name</span> <span class="o">=</span> <span class="n">parser</span><span class="p">.</span><span class="n">getName</span><span class="p">();</span>
</span><span class='line'>        <span class="k">switch</span> <span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;selector&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">StateListDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;animated-selector&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">AnimatedStateListDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;level-list&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">LevelListDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;layer-list&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">LayerDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;transition&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">TransitionDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;ripple&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">RippleDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;color&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ColorDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;shape&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">GradientDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;vector&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">VectorDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;animated-vector&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">AnimatedVectorDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;scale&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ScaleDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;clip&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ClipDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;rotate&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">RotateDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;animated-rotate&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">AnimatedRotateDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;animation-list&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">AnimationDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;inset&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">InsetDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;bitmap&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BitmapDrawable</span><span class="p">(</span><span class="n">r</span><span class="p">);</span>
</span><span class='line'>                <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">!=</span> <span class="n">null</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>                    <span class="p">((</span><span class="n">BitmapDrawable</span><span class="p">)</span> <span class="n">drawable</span><span class="p">).</span><span class="n">setTargetDensity</span><span class="p">(</span><span class="n">r</span><span class="p">.</span><span class="n">getDisplayMetrics</span><span class="p">());</span>
</span><span class='line'>                <span class="p">}</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">case</span> <span class="s">&quot;nine-patch&quot;</span><span class="o">:</span>
</span><span class='line'>                <span class="n">drawable</span> <span class="o">=</span> <span class="n">new</span> <span class="n">NinePatchDrawable</span><span class="p">();</span>
</span><span class='line'>                <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">!=</span> <span class="n">null</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>                    <span class="p">((</span><span class="n">NinePatchDrawable</span><span class="p">)</span> <span class="n">drawable</span><span class="p">).</span><span class="n">setTargetDensity</span><span class="p">(</span><span class="n">r</span><span class="p">.</span><span class="n">getDisplayMetrics</span><span class="p">());</span>
</span><span class='line'>                <span class="p">}</span>
</span><span class='line'>                <span class="k">break</span><span class="p">;</span>
</span><span class='line'>            <span class="k">default</span><span class="o">:</span>
</span><span class='line'>                <span class="n">throw</span> <span class="n">new</span> <span class="n">XmlPullParserException</span><span class="p">(</span><span class="n">parser</span><span class="p">.</span><span class="n">getPositionDescription</span><span class="p">()</span> <span class="o">+</span>
</span><span class='line'>                        <span class="s">&quot;: invalid drawable tag &quot;</span> <span class="o">+</span> <span class="n">name</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="n">drawable</span><span class="p">.</span><span class="n">inflate</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="n">attrs</span><span class="p">,</span> <span class="n">theme</span><span class="p">);</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">drawable</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>1.StateListDrawable</h4>

<h4>2.AnimatedStateListDrawable</h4>

<h4>3.LevelListDrawable</h4>

<h4>4.LayerDrawable</h4>

<h4>5.TransitionDrawable</h4>

<h4>6.RippleDrawable</h4>

<h4>7.ColorDrawable</h4>

<h4>8.GradientDrawable</h4>

<h4>9.VectorDrawable</h4>

<ul>
<li>1.vector图像可以自动进行适配，不需要通过分辨率来设置不同的图片Vector图像可以大幅度减少图像的体积。</li>
<li>2.svg图像导出工具方便，从而转换成Vector图像功能强大，前端已经广泛使用。</li>
<li>3.android5.0开始支持矢量图，它非常适合图标，是独立于屏幕密度的，一个文件适合所有屏幕。当缩放的时候还可以保存所有的图片质量，通常文件所占用的空间也非常小使用VectorDrawable 可以大幅度的减少apk大小。最新的AppCompat23.2库通过使用VectorDrawableCompat可以支持在android api7+版本上使用矢量图</li>
</ul>


<p>gem install bundler <a href="https://ruby.taobao.org">https://ruby.taobao.org</a></p>

<h4>10.AnimatedVectorDrawable</h4>

<h4>11.ScaleDrawable</h4>

<h4>12.ClipDrawable</h4>

<h4>13.RotateDrawable</h4>

<h4>14.AnimatedRotateDrawable</h4>

<h4>15.AnimationDrawable</h4>

<h4>16.InsetDrawable</h4>

<h4>17.BitmapDrawable</h4>

<h4>18.NinePatchDrawable</h4>

<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/buildscript.png" alt="image" /></p>

<p>编译成功后：</p>

<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/buildscriptresult.png" alt="image" /></p>

<h4>3.iOS工程集成。</h4>

<h5>1.头文件路径设置。</h5>

<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/libs.png" alt="image" /></p>

<h5>1.依赖库设置。</h5>

<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/pathsetting.png" alt="image" /></p>

<h4>4.编译成功。</h4>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2017/04/22/iOS-xia-shi-yong-ffmpeg/">iOS下使用ffmpeg</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2017-04-22T11:55:00+08:00" pubdate data-updated="true">Apr 22<span>nd</span>, 2017</time>
        
         | <a href="/blog/2017/04/22/iOS-xia-shi-yong-ffmpeg/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>1.<a href="https://github.com/mansr/gas-preprocessor">编译FFmpeg所需要的脚本文件gas-preprocessor.pl</a></h4>

<p>将脚本文件gas-preprocessor.pl复制到 <em>/usr/bin/</em>目录</p>

<h4>2.<a href="https://github.com/kewlbear/FFmpeg-iOS-build-script">iOS平台下编译ffmpeg的脚本</a></h4>

<p>执行<strong>build-ffmpeg.sh</strong>脚本。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'> <span class="n">sudo</span> <span class="p">.</span><span class="o">/</span><span class="n">build</span><span class="o">-</span><span class="n">ffmpeg</span><span class="p">.</span><span class="n">sh</span>
</span></code></pre></td></tr></table></div></figure>


<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/buildscript.png" alt="image" /></p>

<p>编译成功后：</p>

<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/buildscriptresult.png" alt="image" /></p>

<h4>3.iOS工程集成。</h4>

<h5>1.头文件路径设置。</h5>

<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/libs.png" alt="image" /></p>

<h5>1.依赖库设置。</h5>

<p><img src="/images/post/2017-4-22-iOS-xia-shi-yong-ffmpeg/pathsetting.png" alt="image" /></p>

<h4>4.编译成功。</h4>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/08/19/ios-you-xian-hou-tai-ren-wu/">iOS有限后台任务</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-08-19T20:37:00+08:00" pubdate data-updated="true">Aug 19<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/08/19/ios-you-xian-hou-tai-ren-wu/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>iOS应用，在切换到后台时，可以开启有限后台任务。
见<a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/">苹果开发文档</a>，</p>

<h4>一.几个相关方法</h4>

<ul>
<li><p>1.beginBackgroundTaskWithExpirationHandler</p></li>
<li><p>2.beginBackgroundTaskWithName:expirationHandler</p></li>
<li>3.endBackgroundTask</li>
<li>4.setMinimumBackgroundFetchInterval</li>
</ul>


<h4>二.切换到后台的处理</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">applicationDidEnterBackground:</span><span class="p">(</span><span class="n">UIApplication</span> <span class="o">*</span><span class="p">)</span><span class="nv">application</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">self</span><span class="p">.</span><span class="n">backgroundTaskIdentifier</span> <span class="o">=</span>
</span><span class='line'>    <span class="p">[</span><span class="n">application</span> <span class="nl">beginBackgroundTaskWithExpirationHandler:</span><span class="o">^</span><span class="p">{</span>
</span><span class='line'>        <span class="p">[</span><span class="n">application</span> <span class="nl">endBackgroundTask:</span><span class="n">self</span><span class="p">.</span><span class="n">backgroundTaskIdentifier</span><span class="p">];</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">backgroundTaskIdentifier</span> <span class="o">=</span> <span class="n">UIBackgroundTaskInvalid</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}];</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">//添加自己的处理逻辑</span>
</span><span class='line'>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>三.切换到前台的处理</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">applicationWillEnterForeground:</span><span class="p">(</span><span class="n">UIApplication</span> <span class="o">*</span><span class="p">)</span><span class="nv">application</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>   <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">backgroundTaskIdentifier</span> <span class="o">!=</span> <span class="n">UIBackgroundTaskInvalid</span><span class="p">){</span>
</span><span class='line'>         <span class="p">[</span><span class="n">application</span> <span class="nl">endBackgroundTask:</span><span class="n">self</span><span class="p">.</span><span class="n">backgroundTaskIdentifier</span><span class="p">];</span>
</span><span class='line'>         <span class="n">self</span><span class="p">.</span><span class="n">backgroundTaskIdentifier</span> <span class="o">=</span> <span class="n">UIBackgroundTaskInvalid</span><span class="p">;</span>
</span><span class='line'>   <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/08/19/android-xia-jie-ya-suo/">Android下gzip数据流解压缩</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-08-19T20:21:00+08:00" pubdate data-updated="true">Aug 19<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/08/19/android-xia-jie-ya-suo/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>在Android开发中，网络传输的数据如果采用压缩的方式，能大大减少网络流量，对文本的数据效果更明显。</p>

<h4>一. gzip压缩.</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'>  <span class="n">public</span> <span class="k">static</span> <span class="n">byte</span><span class="p">[]</span> <span class="n">gzipCompress</span><span class="p">(</span><span class="n">final</span> <span class="n">byte</span><span class="p">[]</span> <span class="n">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="mi">0</span> <span class="o">==</span> <span class="n">data</span><span class="p">.</span><span class="n">length</span><span class="p">){</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">KEmptyByteArray</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>        <span class="n">byte</span><span class="p">[]</span> <span class="n">pBuf</span> <span class="o">=</span> <span class="n">KEmptyByteArray</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>        <span class="n">try</span> <span class="p">{</span>
</span><span class='line'>            <span class="n">ByteArrayOutputStream</span> <span class="n">byteArrayOutputStream</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ByteArrayOutputStream</span><span class="p">();</span>
</span><span class='line'>            <span class="n">GZIPOutputStream</span> <span class="n">gzip</span> <span class="o">=</span> <span class="n">new</span> <span class="n">GZIPOutputStream</span><span class="p">(</span><span class="n">byteArrayOutputStream</span><span class="p">);</span>
</span><span class='line'>            <span class="n">gzip</span><span class="p">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
</span><span class='line'>            <span class="n">gzip</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span><span class='line'>            <span class="n">pBuf</span> <span class="o">=</span> <span class="n">byteArrayOutputStream</span><span class="p">.</span><span class="n">toByteArray</span><span class="p">();</span>
</span><span class='line'>            <span class="n">byteArrayOutputStream</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span><span class='line'>        <span class="p">}</span> <span class="n">catch</span> <span class="p">(</span><span class="n">IOException</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>            <span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">pBuf</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>二. gzip解压.</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'>    <span class="n">public</span> <span class="k">static</span> <span class="n">byte</span><span class="p">[]</span> <span class="n">gzipUncompress</span><span class="p">(</span><span class="n">final</span> <span class="n">byte</span><span class="p">[]</span> <span class="n">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">byte</span><span class="p">[]</span> <span class="n">pBuf</span> <span class="o">=</span> <span class="n">KEmptyByteArray</span><span class="p">;</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="n">data</span><span class="p">.</span><span class="n">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>             <span class="n">ByteArrayInputStream</span> <span class="n">byteArrayInputStream</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ByteArrayInputStream</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
</span><span class='line'>            <span class="n">final</span> <span class="n">ByteArrayOutputStream</span> <span class="n">byteArrayOutputStream</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ByteArrayOutputStream</span><span class="p">();</span>
</span><span class='line'>            <span class="n">GZIPInputStream</span> <span class="n">gzipInputStream</span> <span class="o">=</span> <span class="n">null</span><span class="p">;</span>
</span><span class='line'>            <span class="n">try</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>                <span class="n">gzipInputStream</span> <span class="o">=</span> <span class="n">new</span> <span class="n">GZIPInputStream</span><span class="p">(</span><span class="n">byteArrayInputStream</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>                <span class="kt">int</span> <span class="n">byteCount</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>                <span class="k">while</span> <span class="p">(</span><span class="n">byteCount</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>                    <span class="n">byteCount</span> <span class="o">=</span> <span class="n">gzipInputStream</span><span class="p">.</span><span class="n">read</span><span class="p">(</span><span class="n">KGZipUncompressBuffer</span><span class="p">);</span>
</span><span class='line'>                    <span class="k">if</span><span class="p">(</span><span class="n">byteCount</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">){</span>
</span><span class='line'>                        <span class="n">byteArrayOutputStream</span><span class="p">.</span><span class="n">write</span><span class="p">(</span><span class="n">KGZipUncompressBuffer</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">byteCount</span><span class="p">);</span>
</span><span class='line'>                    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>                <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>                <span class="n">pBuf</span> <span class="o">=</span> <span class="n">byteArrayOutputStream</span><span class="p">.</span><span class="n">toByteArray</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>            <span class="p">}</span><span class="n">catch</span><span class="p">(</span><span class="n">IOException</span> <span class="n">e</span><span class="p">){</span>
</span><span class='line'>                <span class="n">Log</span><span class="p">.</span><span class="n">w</span><span class="p">(</span><span class="s">&quot;gzip uncompressed fail&quot;</span><span class="p">,</span> <span class="n">e</span><span class="p">.</span><span class="n">getMessage</span><span class="p">());</span>
</span><span class='line'>            <span class="p">}</span>
</span><span class='line'>            <span class="n">finally</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>                <span class="n">try</span> <span class="p">{</span>
</span><span class='line'>                    <span class="k">if</span> <span class="p">(</span><span class="n">null</span> <span class="o">!=</span><span class="n">byteArrayInputStream</span><span class="p">){</span>
</span><span class='line'>                        <span class="n">byteArrayInputStream</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span><span class='line'>                    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>                    <span class="k">if</span> <span class="p">(</span><span class="n">null</span> <span class="o">!=</span> <span class="n">byteArrayOutputStream</span><span class="p">){</span>
</span><span class='line'>                        <span class="n">byteArrayOutputStream</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span><span class='line'>                    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>                    <span class="k">if</span> <span class="p">(</span><span class="n">null</span> <span class="o">!=</span> <span class="n">gzipInputStream</span><span class="p">){</span>
</span><span class='line'>                        <span class="n">gzipInputStream</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span><span class='line'>                    <span class="p">}</span>
</span><span class='line'>                <span class="p">}</span>
</span><span class='line'>                <span class="n">catch</span> <span class="p">(</span><span class="n">IOException</span> <span class="n">e</span><span class="p">){</span>
</span><span class='line'>                    <span class="n">Log</span><span class="p">.</span><span class="n">w</span><span class="p">(</span><span class="s">&quot;gzip&quot;</span><span class="p">,</span> <span class="n">e</span><span class="p">.</span><span class="n">getMessage</span><span class="p">());</span>
</span><span class='line'>                <span class="p">}</span>
</span><span class='line'>            <span class="p">}</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">pBuf</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h3>三.gzip数据的判断</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">//判断一个数据流是否是GZip</span>
</span><span class='line'><span class="n">public</span> <span class="k">static</span> <span class="n">boolean</span> <span class="nf">isGZipData</span><span class="p">(</span><span class="n">final</span> <span class="n">byte</span><span class="p">[]</span> <span class="n">data</span><span class="p">){</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">data</span><span class="p">.</span><span class="n">length</span> <span class="o">&gt;</span> <span class="mi">2</span>
</span><span class='line'>            <span class="o">&amp;&amp;</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">KYKGZipHeaderFirstByte</span>
</span><span class='line'>            <span class="o">&amp;&amp;</span> <span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">KYKGZipHeaderSecondByte</span><span class="p">){</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">true</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="n">false</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h3>四.一些点说明</h3>

<h5>1.KGZipUncompressBuffer</h5>

<p>KGZipUncompressBuffer是一固定大小的内存缓冲区，减少内存的频繁创建与过大内存的分配。</p>

<h4>2.GZip数据流识别</h4>

<p>KYKGZipHeaderFirstByte|KYKGZipHeaderSecondByte：第一个字节0x1f(31),第二个字节为0x8b(139),标识为GZIP格式</p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/08/19/facebook-infer-de-shi-yong/">FaceBook-infer的使用</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-08-19T19:52:00+08:00" pubdate data-updated="true">Aug 19<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/08/19/facebook-infer-de-shi-yong/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><strong>Facebook</strong>推出潜在一款<strong>bug</strong>扫描工具(<a href="http://fbinfer.com">http://fbinfer.com</a>)，可以检查 <strong>iOS/Android</strong> App.</p>

<p>MAC上的安装</p>

<h3>一.环境要求</h3>

<ul>
<li>1.Python版本大于等于2.7</li>
<li>2.<a href="https://opam.ocaml.org/doc/Install.html#OSX">opam</a></li>
<li>3.对C/Objective-C分析要求

<ul>
<li> XCode &lt;= 6.3, >= 6.1</li>
<li>clang (in XCode command line tools. You can install them with the command xcode-select &mdash;install)</li>
</ul>
</li>
</ul>


<h3>二.安装步骤</h3>

<h4>1.安装opam。</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">brew</span> <span class="n">install</span> <span class="n">opam</span>   <span class="err">#</span> <span class="n">Homebrew</span><span class="p">,</span> <span class="n">OSX</span> <span class="n">Mavericks</span> <span class="n">or</span> <span class="n">later</span>
</span><span class='line'>
</span><span class='line'><span class="n">opam</span> <span class="n">init</span> <span class="o">--</span><span class="n">comp</span><span class="o">=</span><span class="mf">4.01.0</span>  <span class="err">#</span> <span class="p">(</span><span class="n">answer</span> <span class="sc">&#39;y&#39;</span> <span class="n">to</span> <span class="n">the</span> <span class="n">question</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">eval</span> <span class="err">\`</span><span class="n">opam</span> <span class="n">config</span> <span class="n">env</span><span class="err">\`</span> <span class="err">#去掉前面</span>\
</span><span class='line'>
</span><span class='line'><span class="n">opam</span> <span class="n">install</span> <span class="n">extlib</span><span class="mf">.1.5.4</span> <span class="n">atdgen</span><span class="mf">.1.6.0</span> <span class="n">javalib</span><span class="mf">.2.3.1</span> <span class="n">sawja</span><span class="mf">.1.5.1</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>2.对C/Objective-C的分析支持。</h4>

<p>如果你既想支持Java,也想支持C/Objective-C,那么换下面的方式执行</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">cd</span> <span class="n">infer</span>
</span><span class='line'><span class="p">.</span><span class="o">/</span><span class="n">update</span><span class="o">-</span><span class="n">fcp</span><span class="p">.</span><span class="n">sh</span> <span class="o">&amp;&amp;</span> <span class="p">..</span><span class="o">/</span><span class="n">facebook</span><span class="o">-</span><span class="n">clang</span><span class="o">-</span><span class="n">plugin</span><span class="o">/</span><span class="n">clang</span><span class="o">/</span><span class="n">setup</span><span class="p">.</span><span class="n">sh</span> <span class="o">&amp;&amp;</span> <span class="p">.</span><span class="o">/</span><span class="n">compile</span><span class="o">-</span><span class="n">fcp</span><span class="p">.</span><span class="n">sh</span> <span class="err">#</span> <span class="n">go</span> <span class="n">have</span> <span class="n">a</span> <span class="n">coffee</span> <span class="o">:</span><span class="p">)</span>
</span><span class='line'><span class="n">make</span> <span class="o">-</span><span class="n">C</span> <span class="n">infer</span>
</span><span class='line'><span class="n">export</span> <span class="n">PATH</span><span class="o">=</span><span class="err">\`</span><span class="n">pwd</span><span class="err">\`</span><span class="o">/</span><span class="n">infer</span><span class="o">/</span><span class="nl">bin:</span><span class="n">$PATH</span> <span class="err">#去掉前面</span>\
</span></code></pre></td></tr></table></div></figure>


<p></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/08/12/uiwebview-zi-yuan-wen-jian-huan-cun/">UIWebView资源文件缓存</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-08-12T19:52:00+08:00" pubdate data-updated="true">Aug 12<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/08/12/uiwebview-zi-yuan-wen-jian-huan-cun/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content">
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/08/10/ios-luada-bu-ding/">iOS-lua打补丁</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-08-10T11:33:00+08:00" pubdate data-updated="true">Aug 10<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/08/10/ios-luada-bu-ding/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>一.目的</h3>

<h5>1.更改线上App小的功能点。</h5>

<h4>2.修复线上App的Bug。</h4>

<h4>3.对于iOS来讲，避免了漫长的AppStore审核。</h4>

<h3>二.方案。</h3>

<p> <img src="/images/post/2015-08-10-ios-luada-bu-ding/diagram_overview.jpg" alt="image" /></p>

<h3>三.接口</h3>

<h4>loadPatch</h4>

<h5>参数：</h5>

<ul>
<li>平台：OSType(int)

<ul>
<li>0表示无意义</li>
<li>1表示iOS</li>
<li>2表示Android</li>
</ul>
</li>
<li>App版本号:appVersion(String)</li>
</ul>


<h5>实体数据：</h5>

<ul>
<li><p>补丁包Id：patchId(若无补丁包，空串;如果非空串，有补丁包。)</p>

<pre><code>  客户端如果发现本次patchId与上次保存补丁id不一致，即下载该补丁包。如果一致，无需重复下载，直接使用上次补丁包即可。
</code></pre></li>
<li>补丁包地址:patchUrl(String)</li>
</ul>


<h4>四.iOS具体方案</h4>

<h5>1.采用lua与objective-c交互的方式，通过lua脚本替换objective-c函数的实现。</h5>

<h5>2.通过Wax加载后改变了既有Objective-C实现方法的指向函数，从而改变了程序的行为。</h5>

<h5>3.示例</h5>

<p><strong>patch.lua</strong>代码如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">require</span> <span class="s">&quot;YKHomeViewController&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>YKHomeViewController.lua</strong>代码如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">waxClass</span><span class="p">{</span><span class="s">&quot;YKHomeViewController&quot;</span><span class="p">,</span> <span class="n">UIViewController</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">function</span> <span class="n">loadCache</span><span class="p">(</span><span class="n">self</span><span class="p">)</span>
</span><span class='line'>  <span class="n">local</span> <span class="n">localCache</span> <span class="o">=</span> <span class="nl">YKLocalCache:</span><span class="n">sharedLocalCache</span><span class="p">()</span>
</span><span class='line'>  <span class="n">local</span>  <span class="n">userModel</span> <span class="o">=</span> <span class="nl">YKUserManager:</span><span class="n">sharedYKUserManager</span><span class="p">()</span><span class="o">:</span><span class="n">currentUser</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">local</span> <span class="n">cacheData</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span>  <span class="n">userModel</span>  <span class="n">and</span> <span class="nl">userModel:</span><span class="n">userId</span><span class="p">()</span> <span class="n">then</span>
</span><span class='line'>      <span class="n">cacheData</span> <span class="o">=</span> <span class="nl">localCache:</span><span class="n">cacheFromLocalFile_withUserId</span><span class="p">(</span><span class="s">&quot;KYKLocalCacheHomeLogin&quot;</span><span class="p">,</span><span class="nl">userModel:</span><span class="n">userId</span><span class="p">())</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>      <span class="n">cacheData</span> <span class="o">=</span> <span class="nl">localCache:</span><span class="n">cacheFromLocalFile</span><span class="p">(</span><span class="s">&quot;KYKLocalCacheHomeUnLogin&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="n">end</span>  
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span>  <span class="n">cacheData</span> <span class="n">and</span> <span class="nl">cacheData:</span><span class="n">length</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">0</span>   <span class="n">then</span>
</span><span class='line'>      <span class="n">local</span> <span class="n">jsonDic</span> <span class="o">=</span> <span class="nl">NSJSONSerialization:</span><span class="n">JSONObjectWithData_options_error</span><span class="p">(</span><span class="n">cacheData</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">nil</span><span class="p">);</span>
</span><span class='line'>      <span class="k">if</span> <span class="n">jsonDic</span> <span class="n">then</span>
</span><span class='line'>          <span class="nl">self:</span><span class="n">handleHomeParser_withResponseData</span><span class="p">(</span><span class="n">jsonDic</span><span class="p">,</span><span class="n">cacheData</span><span class="p">)</span>
</span><span class='line'>      <span class="n">end</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>      <span class="nl">self:</span><span class="n">setupRefreshTableViewDataArrayData</span><span class="p">()</span>
</span><span class='line'>  <span class="n">end</span>          
</span><span class='line'><span class="n">end</span>
</span></code></pre></td></tr></table></div></figure>


<h5>4.简单脚本把文件压缩</h5>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="cp">#!/bin/bash</span>
</span><span class='line'>
</span><span class='line'><span class="n">zip</span> <span class="o">-</span><span class="n">r</span>  <span class="n">lua</span><span class="p">.</span><span class="n">zip</span>  <span class="n">lua</span>
</span></code></pre></td></tr></table></div></figure>


<h4>!!!</h4>

<ul>
<li>1.注意不要用zip工具压缩，不然，解压时会失败。</li>
<li>2.64位的适配参考<a href="https://github.com/maxfong/WaxPatch_X64">https://github.com/maxfong/WaxPatch_X64</a></li>
</ul>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/05/14/ios-lou-kong-uiview/">iOS镂空UIView</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-05-14T22:46:00+08:00" pubdate data-updated="true">May 14<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/05/14/ios-lou-kong-uiview/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>如果想把UIView挖空一块，使用遮罩图。效果图如下：</p>

<p><img src="/images/post/2015-05-14-ios-lou-kong-uiview/overview.png" alt="image" /></p>

<p>示例代码如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">viewDidAppear:</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nv">animated</span><span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">super</span> <span class="nl">viewDidAppear:</span><span class="n">animated</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">CALayer</span> <span class="o">*</span><span class="n">mask</span> <span class="o">=</span> <span class="p">[</span><span class="n">CALayer</span> <span class="n">layer</span><span class="p">];</span>
</span><span class='line'>    <span class="n">mask</span><span class="p">.</span><span class="n">frame</span> <span class="o">=</span> <span class="n">self</span><span class="p">.</span><span class="n">maskView</span><span class="p">.</span><span class="n">bounds</span><span class="p">;</span>
</span><span class='line'>    <span class="n">mask</span><span class="p">.</span><span class="n">contents</span> <span class="o">=</span> <span class="p">(</span><span class="kt">id</span><span class="p">)[[</span><span class="n">UIImage</span> <span class="nl">imageNamed:</span><span class="s">@&quot;mask&quot;</span><span class="p">]</span> <span class="n">CGImage</span><span class="p">];</span>
</span><span class='line'>    <span class="n">_maskView</span><span class="p">.</span><span class="n">layer</span><span class="p">.</span><span class="n">mask</span> <span class="o">=</span> <span class="n">mask</span><span class="p">;</span>
</span><span class='line'>    <span class="n">_maskView</span><span class="p">.</span><span class="n">layer</span><span class="p">.</span><span class="n">masksToBounds</span> <span class="o">=</span> <span class="n">YES</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>代码见<a href="https://github.com/ksnowlv/maskTest.git">链接</a></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/05/08/java-slash-android-shuang-zhong-suo-ding-mo-shi/">Java/Android双重锁定模式</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-05-08T17:51:00+08:00" pubdate data-updated="true">May 8<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/05/08/java-slash-android-shuang-zhong-suo-ding-mo-shi/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><strong>双重检查锁定模式</strong>(<em>DCLP</em>)是一种软件设计模式,用来减少并发系统中竞争和同步的开销.</p>

<h5>适用性方面：在J2SE 5.0之前的版本使用，有隐患。在J2SE 5.0开始的版本，可以放心使用，已经解决了原有的问题。可参考链接<a href="http://zh.wikipedia.org/zh-cn/%E5%8F%8C%E9%87%8D%E6%A3%80%E6%9F%A5%E9%94%81%E5%AE%9A%E6%A8%A1%E5%BC%8F">双重检查锁定模式</a></h5>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'> <span class="kd">private</span> <span class="kd">volatile</span> <span class="kd">static</span> <span class="n">ImageLoader</span> <span class="n">instance</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="cm">/** Returns singleton class instance */</span>
</span><span class='line'>  <span class="kd">public</span> <span class="kd">static</span> <span class="n">ImageLoader</span> <span class="nf">getInstance</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="o">(</span><span class="n">instance</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>          <span class="kd">synchronized</span> <span class="o">(</span><span class="n">ImageLoader</span><span class="o">.</span><span class="na">class</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>              <span class="k">if</span> <span class="o">(</span><span class="n">instance</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>                  <span class="n">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ImageLoader</span><span class="o">();</span>
</span><span class='line'>              <span class="o">}</span>
</span><span class='line'>          <span class="o">}</span>
</span><span class='line'>      <span class="o">}</span>
</span><span class='line'>      <span class="k">return</span> <span class="n">instance</span><span class="o">;</span>
</span><span class='line'>  <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">//构造函数私有</span>
</span><span class='line'>  <span class="kd">protected</span> <span class="nf">ImageLoader</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>  <span class="o">}</span>
</span><span class='line'>  
</span></code></pre></td></tr></table></div></figure>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/04/28/android-xian-cheng-chi/">Android线程池</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-04-28T23:57:00+08:00" pubdate data-updated="true">Apr 28<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/04/28/android-xian-cheng-chi/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><strong>ExecutorService</strong>继承<strong>Executor</strong>，是线程池的一个服务，可以随时关闭线程池。</p>

<p><strong>Executors</strong>是个工厂类，专门创建各种线程池。</p>

<h4>一.Android常用的线程池有以下四种</h4>

<h5>1.newFixedThreadPool</h5>

<pre><code>创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待.
</code></pre>

<h5>2.newSingleThreadExecutor</h5>

<pre><code>创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务，保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行
</code></pre>

<h5>3.newScheduledThreadPool</h5>

<pre><code>创建一个定长线程池，支持定时及周期性任务执行
</code></pre>

<h5>4.newCachedThreadPool</h5>

<pre><code>创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程.
</code></pre>

<h4>二.new Thread的缺点：</h4>

<h5>1.性能变差</h5>

<pre><code>每次new Thread新建线程，导致性能变差。
</code></pre>

<h4>2.线程缺乏统一调度</h4>

<pre><code>可能无节制的频繁创建线程，无法复用；相互之间竞争，及可能占用过多系统资源导致死机或oom。
</code></pre>

<h4>三.Executors线程池优点：</h4>

<h4>1.重用线程对象</h4>

<pre><code>减少线程对象创建、销毁的开销。
</code></pre>

<h4>2.有效控制线程的系统开销</h4>

<ul>
<li>可以根据需要控制线程数最大并发数。</li>
<li>提高系统资源的使用率，降低内存/CPU占用，避免过多资源竞争，避免堵塞。</li>
</ul>


<h4>3.提供定时执行、定期执行、单线程、并发数控制等功能。</h4>

<h4>四.线程沲实质：一种特殊的对象池</h4>
</div>
  
  


    </article>
  
  <div class="pagination">
    
      <a class="prev" href="/blog/page/2/">&larr; Older</a>
    
    <a href="/blog/archives">Blog Archives</a>
    
  </div>
</div>
<aside class="sidebar">
  
    <section>
  <h1>关于我</h1>
  <p>律威,QQ:309235009 微信:ksnowlv</p>
  <p>手机单机游戏/手机网游封神online/手机阅读/腾讯iOS地图客户端&实时公交&开放API(地图&街景)/iPad/iPhone淘宝客户端及性能优化</p>
  <p>目前在银客做银客/简理财/&#8230;</p>
  <p>能做一点,是一点;不求大成,不求一万年;积少成多,水到渠成!</p>
</section>
<section>
  <h1>最近发布</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2017/04/26/android-drawable/">Android drawable</a>
      </li>
    
      <li class="post">
        <a href="/blog/2017/04/22/iOS-xia-shi-yong-ffmpeg/">iOS下使用ffmpeg</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/ios-you-xian-hou-tai-ren-wu/">iOS有限后台任务</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/android-xia-jie-ya-suo/">Android下gzip数据流解压缩</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/facebook-infer-de-shi-yong/">FaceBook-infer的使用</a>
      </li>
    
  </ul>
</section>
<section>
  <h1>分类</h1>
    <ul id="category-list"><li><a href='/blog/categories/android-jie-ya-suo/'>Android-解压缩 (1)</a></li><li><a href='/blog/categories/androidduo-xian-cheng/'>Android多线程 (3)</a></li><li><a href='/blog/categories/androidcha-jian/'>Android插件 (1)</a></li><li><a href='/blog/categories/androidhui-tu-./'>Android绘图。 (1)</a></li><li><a href='/blog/categories/androidwang-luo/'>Android网络 (1)</a></li><li><a href='/blog/categories/git/'>git (6)</a></li><li><a href='/blog/categories/githubbo-ke/'>github博客 (8)</a></li><li><a href='/blog/categories/ios/'>iOS (6)</a></li><li><a href='/blog/categories/ios-block/'>iOS-block (1)</a></li><li><a href='/blog/categories/ios-bug/'>iOS-Bug (2)</a></li><li><a href='/blog/categories/ios-memory/'>iOS-Memory (4)</a></li><li><a href='/blog/categories/ios-push/'>iOS-push (1)</a></li><li><a href='/blog/categories/ios-ui/'>iOS-UI (2)</a></li><li><a href='/blog/categories/ios-you-hua/'>iOS-优化 (1)</a></li><li><a href='/blog/categories/ios-nei-cun/'>iOS-内存 (1)</a></li><li><a href='/blog/categories/ios-ji-chu/'>iOS-基础 (3)</a></li><li><a href='/blog/categories/ios-duo-xian-cheng/'>iOS-多线程 (3)</a></li><li><a href='/blog/categories/ios-cun-chu/'>iOS-存储 (1)</a></li><li><a href='/blog/categories/ios-an-quan/'>iOS-安全 (1)</a></li><li><a href='/blog/categories/ios-wang-luo/'>iOS-网络 (13)</a></li><li><a href='/blog/categories/ios-jie-ya-suo/'>iOS-解压缩 (1)</a></li><li><a href='/blog/categories/ios-she-ji-mo-shi/'>iOS-设计模式 (1)</a></li><li><a href='/blog/categories/ios-diao-shi/'>iOS-调试 (1)</a></li><li><a href='/blog/categories/iosyou-hua/'>iOS优化 (1)</a></li><li><a href='/blog/categories/iosji-chu/'>iOS基础 (2)</a></li><li><a href='/blog/categories/iosyin-pin/'>iOS音频 (1)</a></li><li><a href='/blog/categories/mac/'>Mac (4)</a></li><li><a href='/blog/categories/mac-duo-xian-cheng/'>Mac-多线程 (1)</a></li><li><a href='/blog/categories/nginx/'>nginx (3)</a></li><li><a href='/blog/categories/php/'>php (3)</a></li><li><a href='/blog/categories/python/'>python (12)</a></li><li><a href='/blog/categories/swift/'>swift (11)</a></li><li><a href='/blog/categories/uml/'>UML (8)</a></li><li><a href='/blog/categories/xcode/'>Xcode (11)</a></li><li><a href='/blog/categories/chan-pin-she-ji/'>产品设计 (2)</a></li><li><a href='/blog/categories/gong-ju/'>工具 (2)</a></li><li><a href='/blog/categories/shu-ju-ku/'>数据库 (2)</a></li><li><a href='/blog/categories/sheng-huo-pian-duan/'>生活片段 (10)</a></li><li><a href='/blog/categories/zhi-ye-sheng-ya/'>职业生涯 (7)</a></li><li><a href='/blog/categories/du-shu/'>读书 (2)</a></li></ul>
</section>
<section>
  <h1>外部链接</h1>
    <ul id="外部链接">
    	<li><a href="https://developer.apple.com">苹果开发者中心</a> </li>
    	<li><a href="http://stackoverflow.com/questions/tagged/ios">stackoverflow</a></li>
    	<li><a href="http://ksnowlv.blog.163.com/blog/#m=0">ksnowlv网易博客</a></li>
	</ul>
</section>

  
</aside>

    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 律威 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>

  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253538110'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/z_stat.php%3Fid%3D1253538110%26online%3D1' type='text/javascript'%3E%3C/script%3E"));</script>

  <script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F4c1727a94ea20751730e8a556dbdbf82' type='text/javascript'%3E%3C/script%3E"));
</script>

</p>



</footer>
  

<script type="text/javascript">
      var disqus_shortname = 'ksnowlv';
      
        
        var disqus_script = 'count.js';
      
    (function () {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    }());
</script>








</body>
</html>
